/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TKHangHoa.java
 *
 * Created on Jan 25, 2013, 10:17:34 PM
 */
package quanlykho;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.print.attribute.standard.DateTimeAtCompleted;
import javax.swing.JComboBox;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author User
 */
public class TKHangHoa extends javax.swing.JPanel {

    /** Creates new form TKHangHoa */ 
       DefaultTableModel model;
    DefaultTableModel model2;
    DefaultTableModel model1;
    public TKHangHoa() {
        initComponents();
        
        Ketnoi a = new Ketnoi();
        Connection con;
        con = a.connect();
        String lh ="loaihang";
            loadCb(jc1,lh,2);
            model1 = (DefaultTableModel) jt3.getModel();
        try {
            model = (DefaultTableModel) jt1.getModel();
            PreparedStatement pst1 = con.prepareCall("Select * from View_PhieuNhap");
            ResultSet rs1 = pst1.executeQuery();
            while (rs1.next()) {
                Vector b = new Vector();
                b.add(rs1.getString(1));
                b.add(rs1.getString(2));

                b.add(rs1.getString(3));
                b.add(rs1.getString(4));
                b.add(rs1.getInt(5));
                b.add(rs1.getInt(6));
                //model.addRow(b);
            }
              int num = jt1.getRowCount();
            int s1 = 0;
            int s2 = 0;
            int s3 = 0;
            for (int j = num - 1; j >= 0; j--) {
                s1+=Integer.parseInt(jt1.getValueAt(j, 5).toString());    
            }
            nhap.setText(String.valueOf(s1));
            
            model = (DefaultTableModel) jt2.getModel();
            PreparedStatement pst2 = con.prepareCall("Select * from View_PhieuXuat");
            ResultSet rs2 = pst2.executeQuery();
            while (rs2.next()) {
                Vector b = new Vector();
                b.add(rs2.getString(1));
                b.add(rs2.getString(2));
                b.add(rs2.getString(3));
                b.add(rs2.getString(4));
                b.add(rs2.getString(5));
                b.add(rs2.getString(6));
                b.add(rs2.getString(7));
                b.add(rs2.getInt(8));
                b.add(rs2.getInt(9));
                //model.addRow(b);

            }
             num = jt2.getRowCount();
             for (int j = num - 1; j >= 0; j--) {
                s2+=Integer.parseInt(jt2.getValueAt(j, 8).toString());    
            }
           xuat.setText(String.valueOf(s2));
           s3=s2-s1;
           thu.setText(String.valueOf(s3));
             
        } catch (SQLException ex) {
            Logger.getLogger(ThongKeHangHoa.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
     public void loadTable(String str)
    {
        try {
            model1 = (DefaultTableModel) jt3.getModel();
                int num=jt3.getRowCount();
                for (int i = num-1; i >= 0; i--) {
                    model1.removeRow(i);
                }
                Ketnoi a = new Ketnoi();
                Connection con = a.connect();
                PreparedStatement pst1 = con.prepareCall("Select * from loaihang where tenloaihh = ?");
                pst1.setString(1, str);
                ResultSet rs1= pst1.executeQuery();
                String str1= null;
                while(rs1.next())
                {
                    str1=rs1.getString(1);
                }
                System.out.println(str1);
                PreparedStatement pst = con.prepareCall("Select * from hanghoa where maloaihh = ?");
                pst.setString(1, str1);
                ResultSet rs = pst.executeQuery();
                Vector b = null;
                while(rs.next())
                {
                    b=new Vector();
                    b.add(rs.getString(1));
                    b.add(rs.getString(2));
                    model1.addRow(b);
                }
        } catch (SQLException ex) {
            Logger.getLogger(ThongTinHangHoa.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void loadCb(JComboBox cb,String str,int index)
    {
        try {
            Ketnoi a = new Ketnoi();
            Connection con = a.connect();
            cb.removeAllItems();
            cb.addItem("Lựa chọn");
            String str1="Select * from "+str;
            System.out.println(str1);
            PreparedStatement pst = con.prepareCall(str1);
            ResultSet rs = pst.executeQuery();
            while(rs.next())
            {
                cb.addItem(rs.getString(index));  
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(ThongTinHangHoa.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
     public Vector getCb(String str,int index)
    {
        Vector v = new Vector();
        try {
            Ketnoi a = new Ketnoi();
            Connection con = a.connect();
            String str1="Select * from "+str;
            PreparedStatement pst = con.prepareCall(str1);
           ResultSet rs = pst.executeQuery();
            while(rs.next())
            {
                v.add(rs.getString(index));  
            }
            String st = null;
        } catch (SQLException ex) {
            Logger.getLogger(ThongTinHangHoa.class.getName()).log(Level.SEVERE, null, ex);
        }
        return v;
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel7 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        mahh = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jt1 = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jt2 = new javax.swing.JTable();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        labnhap3 = new javax.swing.JLabel();
        lablenhap = new javax.swing.JLabel();
        nhap = new javax.swing.JTextField();
        xuat = new javax.swing.JTextField();
        thu = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        labnhap4 = new javax.swing.JLabel();
        jc1 = new javax.swing.JComboBox();
        jScrollPane3 = new javax.swing.JScrollPane();
        jt3 = new javax.swing.JTable();
        jLabel10 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();

        jLabel5.setText("Mã Hàng Hóa:");

        jt1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Mã Hàng Hóa", "Mã Phiếu Nhập", "Mã Nhân Viên", "Ngày Nhập", "Số Mặt Hàng", "Tổng Tiền"
            }
        ));
        jScrollPane1.setViewportView(jt1);

        jt2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Mã Hàng Hóa", "Mã Phiếu Xuất", "Mã Nhân Viên", "Tên KH", "Địa Chỉ KH", "Số ĐT KH", "Ngày Xuất", "Số Mặt Hàng", "Tổng Tiền"
            }
        ));
        jScrollPane2.setViewportView(jt2);

        jLabel6.setText("Nhập kho:");

        jLabel7.setText("Xuất kho:");

        labnhap3.setText("Tổng tiền Xuất:");

        lablenhap.setText("Tổng tiền Nhập:");

        jButton2.setText("Xem");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        labnhap4.setText("Doanh thu");

        jc1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jc1ActionPerformed(evt);
            }
        });

        jt3.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Mã Hàng", "Tên Hàng"
            }
        ));
        jScrollPane3.setViewportView(jt3);

        jLabel10.setText("Loại hàng :");

        jButton3.setText("Lựa chọn");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel7Layout.createSequentialGroup()
                                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel7Layout.createSequentialGroup()
                                        .addComponent(jLabel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jc1, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(31, 31, 31)
                                        .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addGroup(jPanel7Layout.createSequentialGroup()
                                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(mahh, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jButton2))
                                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 304, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel7Layout.createSequentialGroup()
                                        .addComponent(labnhap3, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(xuat, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jPanel7Layout.createSequentialGroup()
                                        .addComponent(lablenhap, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(nhap, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jPanel7Layout.createSequentialGroup()
                                        .addComponent(labnhap4, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(thu, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 177, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(mahh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2)
                    .addComponent(lablenhap, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labnhap3, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(xuat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(labnhap4, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(thu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton3))
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jc1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(35, 35, 35))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 267, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, 527, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(25, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
         Ketnoi a = new Ketnoi();
        Connection con;
        con = a.connect();
        try {
            model = (DefaultTableModel) jt1.getModel();
            int num = jt1.getRowCount();
            for (int j = num - 1; j >= 0; j--) {
                model.removeRow(j);
            }
            
            
            PreparedStatement pst1 = con.prepareCall("Select * from View_PhieuNhap where MaHH =?");
            pst1.setString(1, mahh.getText());
    
            ResultSet rs1 = pst1.executeQuery();
            while (rs1.next()) {
                Vector b = new Vector();
                b.add(rs1.getString(1));
                b.add(rs1.getString(2));

                b.add(rs1.getString(3));
                b.add(rs1.getString(4));
                b.add(rs1.getInt(5));
                b.add(rs1.getInt(6));
                model.addRow(b);
            }
              num = jt1.getRowCount();
            int s1 = 0;
            int s2 = 0;
            int s3 = 0;
            for (int j = num - 1; j >= 0; j--) {
                s1+=Integer.parseInt(jt1.getValueAt(j, 5).toString());    
            }
            nhap.setText(String.valueOf(s1));
            
            model = (DefaultTableModel) jt2.getModel();
            num = jt2.getRowCount();
            for (int j = num - 1; j >= 0; j--) {
                model.removeRow(j);
            }
            PreparedStatement pst2 = con.prepareCall("Select * from View_PhieuXuat where MaHH =?");
            pst2.setString(1, mahh.getText());
            ResultSet rs2 = pst2.executeQuery();
            while (rs2.next()) {
                Vector b = new Vector();
                b.add(rs2.getString(1));
                b.add(rs2.getString(2));
                b.add(rs2.getString(3));
                b.add(rs2.getString(4));
                b.add(rs2.getString(5));
                b.add(rs2.getString(6));
                b.add(rs2.getString(7));
                b.add(rs2.getInt(8));
                b.add(rs2.getInt(9));
                model.addRow(b);

            }
             num = jt2.getRowCount();
             for (int j = num - 1; j >= 0; j--) {
                s2+=Integer.parseInt(jt2.getValueAt(j, 8).toString());    
            }
           xuat.setText(String.valueOf(s2));
           s3=s2-s1;
           thu.setText(String.valueOf(s3));
             
        } catch (SQLException ex) {
            Logger.getLogger(ThongKeHangHoa.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jc1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jc1ActionPerformed
        // TODO add your handling code here:

        String lh="loaihang";
        String lhang=null;
        Vector t=null;
        //t = new Vector();
        t=getCb(lh,2);
        int luachon=0;
        luachon=jc1.getSelectedIndex();
        if(luachon>0)
        {

            lhang=t.get(luachon-1).toString();
            //System.out.println(luachon+lhang);
            loadTable(lhang);
        }
    }//GEN-LAST:event_jc1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        int num1=jt3.getSelectedRow();
        String str5=null;
        
        if(num1>=0)
        {
            str5 = jt3.getValueAt(num1,0).toString();
            mahh.setText(str5);
        }
    }//GEN-LAST:event_jButton3ActionPerformed
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JComboBox jc1;
    private javax.swing.JTable jt1;
    private javax.swing.JTable jt2;
    private javax.swing.JTable jt3;
    private javax.swing.JLabel lablenhap;
    private javax.swing.JLabel labnhap3;
    private javax.swing.JLabel labnhap4;
    private javax.swing.JTextField mahh;
    private javax.swing.JTextField nhap;
    private javax.swing.JTextField thu;
    private javax.swing.JTextField xuat;
    // End of variables declaration//GEN-END:variables
}
